/**
 * Project Name:mms
 * File Name:MeetingRoomServiceImpl
 * Package Name:com.scb.dev.mms.service.impl
 * Date:2018/12/210:22
 * Copyright (c) 2018, Yuchuan.Dong All Rights Reserved.
 */
package com.scb.dev.mms.service.impl;

import com.scb.dev.mms.common.DataCommon;
import com.scb.dev.mms.common.ResponseResult;
import com.scb.dev.mms.common.ResultType;
import com.scb.dev.mms.common.StatusCode;
import com.scb.dev.mms.dao.MeetingInfoMapper;
import com.scb.dev.mms.dao.MeetingRoomMapper;
import com.scb.dev.mms.dto.BookMeetingDto;
import com.scb.dev.mms.dto.InsertMeetingDto;
import com.scb.dev.mms.pojo.MeetingInfo;
import com.scb.dev.mms.pojo.MeetingRoom;
import com.scb.dev.mms.service.IMeetingRoomService;
import com.scb.dev.mms.util.DateTool;
import com.scb.dev.mms.vo.BookMeetingInfoVo;
import com.scb.dev.mms.vo.MeetingInfoDaoVo;
import com.scb.dev.mms.vo.MeetingRoomVo;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * ClassName: MeetingRoomServiceImpl <br/>
 * @Description: ${todo} ADD REASON(可选). <br/><br/>
 * @Date: 2018/12/2 10:22 <br/> 
 *
 * @author Yuchuan.Dong
 * @version V1.0
 * @since JDK 1.8
 */
@Slf4j
@Service
public class MeetingRoomServiceImpl implements IMeetingRoomService {
    @Resource
    private MeetingRoomMapper meetingRoomMapper;

    @Resource
    private MeetingInfoMapper meetingInfoMapper;

    @Override
    @Transactional
    public ResponseResult queryAvailableMeetingRoomInfo(BookMeetingDto bookMeetingDto) {
        List<MeetingInfoDaoVo> meetingInfoDaoVos = meetingInfoMapper.selectMeetingInfoByMeetingRoomId(bookMeetingDto.getMeetingRoomId());
        Map<String,String> availableTimeMap = new HashMap<>();
        for(int i = 0;i<meetingInfoDaoVos.size();i++){
            MeetingInfoDaoVo meetingInfoDaoVo = meetingInfoDaoVos.get(i);
            String stringDate = meetingInfoDaoVo.getMeeStartTime().substring(0,meetingInfoDaoVo.getMeeStartTime().indexOf(" "));
            if (stringDate.equals(bookMeetingDto.getMeetingBookDate())) {
                String startTime = meetingInfoDaoVo.getMeeStartTime().substring(meetingInfoDaoVo.getMeeStartTime().indexOf(" ")+1);
                String endTime = meetingInfoDaoVo.getMeeEndTime().substring(meetingInfoDaoVo.getMeeEndTime().indexOf(" ")+1);
                availableTimeMap.put(startTime,endTime);
            }
        }
        if (CollectionUtils.isEmpty(availableTimeMap)){
            return new ResponseResult(null, true, ResultType.OPERATION_SUCCESS, StatusCode.OPERATIONSUCCESS);
        }
        return new ResponseResult(availableTimeMap, true, ResultType.OPERATION_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    @Override
    public ResponseResult queryAllMeetingRoom() {
        List<MeetingRoom> meetingRooms = meetingRoomMapper.selectAllMeetingRoom();
        if (CollectionUtils.isEmpty(meetingRooms)){
            return new ResponseResult(null, true, ResultType.FIND_SUCCESS, StatusCode.FINDSUCCESS);
        }
        List<MeetingRoomVo> meetingRoomVos = new ArrayList<>(meetingRooms.size());
        for(MeetingRoom meetingRoom : meetingRooms){
            meetingRoomVos.add(assembleMeetingRoomVo(meetingRoom));
        }
        return new ResponseResult(meetingRoomVos, true, ResultType.OPERATION_SUCCESS, StatusCode.OPERATIONSUCCESS);
    }

    private MeetingRoomVo assembleMeetingRoomVo(MeetingRoom meetingRoom){
        MeetingRoomVo meetingRoomVo = new MeetingRoomVo();
        meetingRoomVo.setMeeRoomId(meetingRoom.getMeeRoomId());
        meetingRoomVo.setMeeRoomName(meetingRoom.getMeeRoomNum()+"-"+meetingRoom.getMeeRoomName());
        meetingRoomVo.setMeeRoomCapacity(meetingRoom.getMeeRoomCapacity());
        return meetingRoomVo;
    }

}
